//
//  HLEM3U8DownloadTask.m
//  DownloadM3U8Tool_Example
//
//  Created by hle1879 on 2020/8/28.
//  Copyright © 2020 134344. All rights reserved.
//

#import "HLEM3U8DownloadTask.h"
#import <objc/runtime.h>

@implementation HLEM3U8DownloadTask

-(instancetype)init{
    self = [super init];
    if (self) {
        self.taskID = -1;//默认
        self.supportBackground = NO;//默认
        self.supportBrokenPointDownload = NO;//默认
        self.downloadRetries = 0;//默认
        self.tsArr = NSArray.new;
        self.tsSizeArr = NSArray.new;
    }
    return self;
}

- (nonnull id)copyWithZone:(nullable NSZone *)zone {
    HLEM3U8DownloadTask *instance = [[HLEM3U8DownloadTask alloc]init];
    if (instance) {
        instance.url = self.url;
        instance.taskID = self.taskID;
        instance.name = [self.name copyWithZone:zone];
        instance.tsArr = [self.tsArr copyWithZone:zone];
        instance.totalSize = self.totalSize;
        instance.uri = [self.uri copyWithZone:zone];
        instance.supportBackground = self.supportBackground;
        instance.downloadRetries = self.downloadRetries;
        instance.supportBrokenPointDownload = self.supportBrokenPointDownload;
        instance.tsSizeArr = [self.tsSizeArr copyWithZone:zone];
    }
    return instance;
}

- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
    HLEM3U8DownloadTask *instance = [[HLEM3U8DownloadTask alloc]init];
    if (instance) {
        instance.url = self.url;
        instance.taskID = self.taskID;
        instance.name = [self.name copyWithZone:zone];
        instance.tsArr = [self.tsArr copyWithZone:zone];
        instance.totalSize = self.totalSize;
        instance.uri = [self.uri copyWithZone:zone];
        instance.supportBackground = self.supportBackground;
        instance.downloadRetries = self.downloadRetries;
        instance.supportBrokenPointDownload = self.supportBrokenPointDownload;
        instance.tsSizeArr = [self.tsSizeArr copyWithZone:zone];
    }
    return instance;
}


-(void)encodeWithCoder:(NSCoder *)aCoder
{
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (int i = 0 ; i< count ; i++) {
        const char * name = ivar_getName(ivarList[i]);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [self valueForKey:key];
        [aCoder encodeObject:value forKey:key];
    }
    free(ivarList);
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        unsigned int count = 0;
        Ivar *ivarList = class_copyIvarList([self class], &count);
        for (int i = 0 ; i< count; i++) {
            const char *name = ivar_getName(ivarList[i]);
            NSString *key = [NSString stringWithUTF8String:name];
            id value = [aDecoder decodeObjectForKey:key];
            [self setValue:value forKey:key];
        }
        free(ivarList);
    }
    return self;
}

- (void)copyPropertys:(HLEM3U8DownloadTask*)task{
    unsigned int count = 0;
    Ivar *ivarList = class_copyIvarList([self class], &count);
    for (int i = 0 ; i< count ; i++) {
        const char * name = ivar_getName(ivarList[i]);
        NSString *key = [NSString stringWithUTF8String:name];
        id value = [task valueForKey:key];
        [self setValue:value forKey:key];
    }
    free(ivarList);
}
@end
